package com.sxfans.encryp.dissymmetry;

import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;

import com.sxfans.encryp.utils.Base64Coder;

public class RSACoder {
	public static final String KEY_ALGORITHM = "RSA";
	//公钥
	private static final String PUBLIC_KEY = "RSAPublicKey";
	//私钥
	private static final String PRIVATE_KEY = "RSAPrivateKey";
	
	private static final int KEY_SIZE = 512;
	
	/**
	 * 私钥解密
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] descryptByPrivateKey(byte[] data,byte[] key) throws Exception{
		PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		//生成私钥
		PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		return cipher.doFinal(data);
	}
	
	/**
	 * 公钥解密
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] decryptByPublicKey(byte[] data,byte[] key) throws Exception
	{
		X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.DECRYPT_MODE, publicKey);
		return cipher.doFinal(data);
	}
	
	/**
	 * 私钥加密
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception 
	 */
	public static byte[] encryptByPrivateKey(byte[] data,byte[] key) throws Exception{
		PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, privateKey);
		return cipher.doFinal(data);
	}
	
	/**
	 * 公钥加密
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception 
	 */
	public static byte[] encryptByPulibcKey(byte[] data,byte[] key) throws Exception{
		X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key);
		KeyFactory keyfactory = KeyFactory.getInstance(KEY_ALGORITHM);
		PublicKey publicKey = keyfactory.generatePublic(x509EncodedKeySpec);
		Cipher cipher = Cipher.getInstance(keyfactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
		return cipher.doFinal(data);
	}
	
	/**
	 * 生成私钥
	 * @param keyMap
	 * @return
	 */
	public static byte[] getPrivateKey(Map<String,Key> keyMap){
		Key key = keyMap.get(PRIVATE_KEY);
		return key.getEncoded();
	}
	
	/**
	 * 生成公钥
	 * @param keyMap
	 * @return
	 */
	public static byte[] getPublicKey(Map<String,Key> keyMap){
		Key key = keyMap.get(PUBLIC_KEY);
		return key.getEncoded();
	}
	
	/**
	 * 初始化密钥
	 * @return
	 * @throws Exception
	 */
	public static Map<String,Key> initKey() throws Exception{
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
		keyPairGenerator.initialize(KEY_SIZE);
		KeyPair keyPair = keyPairGenerator.generateKeyPair();
		RSAPrivateKey privateKey =  (RSAPrivateKey) keyPair.getPrivate();
		RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
		keyPair.getPublic();
		Map<String,Key> map = new HashMap<String,Key>();
		map.put(PRIVATE_KEY, privateKey);
		map.put(PUBLIC_KEY, publicKey);
		return map;
	}
	
	public static void main(String[] args) throws Exception {
		Map<String,Key> keyMap = initKey();
		byte[] privateKey = getPrivateKey(keyMap);
		byte[] publicKey = getPublicKey(keyMap);
		
		byte[] data = "中华人民共和国".getBytes();
		System.out.println("私钥："+Base64Coder.base64Encode(privateKey, false));
		System.out.println("公钥："+Base64Coder.base64Encode(publicKey, false));
		
		byte[] pwdPrivate = encryptByPrivateKey(data,privateKey);
		byte[] pwdPublic = encryptByPulibcKey(data,publicKey);
		System.out.println("私钥加密"+Base64Coder.base64Encode(pwdPrivate, false));
		System.out.println("公钥加密"+Base64Coder.base64Encode(pwdPublic, false));
		
		byte[] pri = decryptByPublicKey(pwdPrivate,publicKey);
		byte[] pub = descryptByPrivateKey(pwdPublic, privateKey);
		
		System.out.println(new String(pri));
		System.out.println(new String(pub));
		
	}
}